﻿using ASmile.Com;
using ASmile.Com.Log;
using ASmile.DBModel;
using ASmile.Extend;
using ASmile.Tools;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace ASmile.Win.Forms
{
    public partial class FrmLog : FrmBase
    {
        public FrmLog()
        {
            InitializeComponent();
        }

        ILogBLL BLLObj { get; } = ObjectFactory.GetObject<ILogBLL>("LogBLL");

        ArgLogBase SearchArg { get; } = new ArgLogBase();

        void RefreshData()
        {
            PageChangedEvent(1, ucPageNav1.OnePageCount, out int total);
            ucPageNav1.InitiControl(total);
        }

        private void FrmLog_Load(object sender, EventArgs e)
        {
            ucPageNav1.PageChangedEvent += PageChangedEvent;

            var timeList = new List<TimeRemark>();
            //timeList.Add(new TimeRemark("1小时内", -1, EmTimeUnit.HH));
            timeList.Add(new TimeRemark("1小时内", -1, EmTimeUnit.HH));
            timeList.Add(new TimeRemark("6小时内", -6, EmTimeUnit.HH));
            timeList.Add(new TimeRemark("1天内", -24, EmTimeUnit.HH));
            timeList.Add(new TimeRemark("7天内", -7, EmTimeUnit.dd));
            timeList.Add(new TimeRemark("自定义", 0, EmTimeUnit.ss));

            tsTimeType.ComboBox.DataSource = timeList;
        }

        private void PageChangedEvent(int pageNo, int pageSize, out int total)
        {
            SearchArg.PageNo = pageNo;
            SearchArg.PageSize = pageSize;
            SearchArg.SearchVal = tsTxtSearch.Text;
            SearchArg.LogFormName = ContainerName;
            if (tsTimeType.SelectedItem is TimeRemark time && time.AddCount != 0)
            {
                SearchArg.StartTime = time.TimeValue;
                SearchArg.EndTime = null;
            }
            else
            {
                SearchArg.StartTime = tsStartTime.Value;
                SearchArg.EndTime = tsEndTime.Value;
            }

            var tb = BLLObj.GetLogTable(SearchArg).data;
            total = tb?.RowTotal ?? 0;
            dgv.DataSource = tb?.ToDataTable();
        }

        private void tsTimeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            tsStartTime.Visible = tsEndTime.Visible = tslbTimeConn.Visible
                = tsTimeType.SelectedIndex == tsTimeType.Items.Count - 1;
        }

        private void tsAdSearch_Click(object sender, EventArgs e)
        {
            ADSearch(SearchArg, RefreshData, dgv);
        }

        private void tsTxtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RefreshData();
            }
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            SearchArg.ClearADList();
            RefreshData();
        }

        private void tsbtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;

            if (ContainerName.StringEquals(LogFormName.LogSql))
            {
                var vm = dgv.GetSelectedClassData<VLog_Sql>();
                var rModel = BLLObj.GetSqlDetail(vm.Id).data;
                FrmLogDetail frm = new FrmLogDetail(rModel.SqlContent);
                frm.ShowDialog();
            }
            else if (ContainerName.StringEquals(LogFormName.App))
            {
                var vm = dgv.GetSelectedClassData<VLog_App>();
                var rModel = BLLObj.GetAppDetail(vm.Id).data;
                string detail;
                if (rModel.Exception.IsEmpty())
                {
                    detail = rModel.Message;
                }
                else
                {
                    detail = rModel.Exception;
                }
                FrmLogDetail frm = new FrmLogDetail(detail);
                frm.ShowDialog();
            }
            else if (ContainerName.StringEquals(LogFormName.HTTP))
            {
                var vm = dgv.GetSelectedClassData<VLog_Http>();

                var rModel = BLLObj.GetHttpDetail(vm.Id).data;
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("请求：");
                sb.AppendLine(rModel.ReqData);
                sb.AppendLine("------------------------------");
                sb.AppendLine("返回：");
                sb.AppendLine(rModel.RetData);
                FrmLogDetail frm = new FrmLogDetail(sb.ToString());
                frm.ShowDialog();
            }
            else
            {
                MessageBox.Show("错误的日志类型");
            }
        }
    }
}
